package org.seedltd.upms.service.impl.detection;

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.seedltd.core.base.BaseServiceImpl;
import org.seedltd.core.utils.BeanUtils;
import org.seedltd.core.mybatis.conditions.ProQuery;
import org.seedltd.upms.mapper.detection.DetectionGradingMapper;
import org.seedltd.upms.model.detection.entity.DetectionGrading;
import org.seedltd.upms.model.detection.form.DetectionGradingForm;
import org.seedltd.upms.model.detection.vo.DetectionGradingVO;
import org.seedltd.upms.service.detection.DetectionGradingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* 评分管理业务实现
* @author laizuan
* @since 2021/08/25
*/
@Service
@AllArgsConstructor
public class DetectionGradingServiceImpl extends BaseServiceImpl<DetectionGradingMapper, DetectionGrading> implements DetectionGradingService {


    @Override
    @Transactional
    public boolean update(DetectionGradingForm form) {

        baseMapper.deleteAll();
        List<DetectionGradingForm.DetectionGradingItemForm> list = form.getList();
        DetectionGrading detectionGrading;
        List<DetectionGrading> gradingList = Lists.newArrayList();
        for (DetectionGradingForm.DetectionGradingItemForm detectionGradingItemForm : list) {
            detectionGrading = new DetectionGrading();
            BeanUtils.copyProperties(detectionGradingItemForm, detectionGrading);
            detectionGrading.setMaxScore(form.getMaxScore());
            detectionGrading.setMiniScore(form.getMiniScore());
            gradingList.add(detectionGrading);
        }

        return this.saveBatch(gradingList);
    }


    @Override
    public List<DetectionGradingVO> listAll() {
        return ProQuery.Builder(baseMapper).build().selectList(DetectionGradingVO.class);
    }
}